import EleBasicSelect from "../ele-basic-select";
import EleProTable from "../ele-pro-table";
var index = {
  name: "EleTableSelect",
  components: { EleBasicSelect, EleProTable },
  props: {
    value: [Number, String, Array],
    multiple: Boolean,
    disabled: Boolean,
    size: String,
    clearable: Boolean,
    collapseTags: Boolean,
    placeholder: String,
    automaticDropdown: Boolean,
    popperClass: String,
    popperOptions: Object,
    popperWidth: {
      type: [String, Number],
      default: 480
    },
    id: String,
    name: String,
    autocomplete: {
      type: String,
      default: "off"
    },
    valueKey: {
      type: String,
      default: "value"
    },
    labelKey: {
      type: String,
      default: "label"
    },
    initValue: [Object, Array],
    tableConfig: {
      type: Object,
      required: true
    }
  },
  emits: [
    "input",
    "change",
    "focus",
    "blur",
    "clear",
    "remove-tag",
    "visible-change"
  ],
  data() {
    return {
      selectedLabel: "",
      selected: [],
      visible: false,
      current: null,
      selection: [],
      noUpdate: false
    };
  },
  computed: {
    selectPopperClass() {
      const temp = ["ele-table-select-dropdown"];
      if (this.popperClass) {
        temp.push(this.popperClass);
      }
      return temp.join(" ");
    }
  },
  methods: {
    updateVisible(value) {
      this.visible = value;
      this.$emit("visible-change", value);
    },
    updateValue(value) {
      const changed = (() => {
        const isEmpty = this.isEmptyValue(this.value);
        if (!this.multiple) {
          if (this.isEmptyValue(value) && isEmpty) {
            return false;
          }
          return value !== this.value;
        }
        if (isEmpty || !this.value.length) {
          return !!value.length;
        } else if (value.length !== this.value.length) {
          return true;
        }
        for (let i = 0; i < value.length; i++) {
          if (!this.value.includes(value[i])) {
            return true;
          }
        }
        return false;
      })();
      if (changed) {
        this.$emit("input", value);
        this.$emit("change", value);
      }
    },
    updateCurrent(current) {
      this.current = current;
      if (!this.multiple && current && !this.noUpdate) {
        this.updateValue(current[this.valueKey]);
      }
    },
    updateSelection(selection) {
      this.selection = selection;
      if (this.multiple && selection && !this.noUpdate) {
        this.updateValue(selection.map((d) => d[this.valueKey]));
      }
    },
    onFocus() {
      this.$emit("focus");
    },
    onBlur() {
      this.$emit("blur");
    },
    onClear() {
      this.updateValue(this.multiple ? [] : null);
      this.$emit("clear");
      this.updateVisible(false);
    },
    onRemoveTag(item) {
      const value = item.value;
      this.updateValue(this.value.filter((v) => v !== value));
      this.$emit("remove-tag", value);
    },
    onValueChange(value) {
      var _a, _b;
      const noInit = this.isEmptyValue(this.initValue);
      if (!this.multiple) {
        if (this.isEmptyValue(value)) {
          this.selectedLabel = "";
          this.updateCurrentRow(null);
          return;
        }
        const row = this.getRowByValue(value);
        if (row) {
          this.selectedLabel = row[this.labelKey];
          if (this.current !== row) {
            this.updateCurrentRow(row);
          }
          return;
        }
        this.selectedLabel = noInit ? value : this.initValue[this.labelKey];
        this.updateCurrentRow(null);
        return;
      }
      if (value == null ? void 0 : value.length) {
        const { checked, addList, removeList } = this.getMultipleResult(value);
        this.selected = checked;
        this.updateRowSelections({ addList, removeList });
      } else {
        this.selected = [];
        this.clearRowSelections();
      }
      (_b = (_a = this.$refs) == null ? void 0 : _a.select) == null ? void 0 : _b.resetInputHeight();
    },
    updateCurrentRow(row) {
      var _a, _b;
      this.noUpdate = true;
      (_b = (_a = this.$refs) == null ? void 0 : _a.table) == null ? void 0 : _b.setCurrentRow(row);
      this.noUpdate = false;
    },
    updateRowSelections({ addList, removeList }) {
      var _a;
      const tableRef = (_a = this.$refs) == null ? void 0 : _a.table;
      this.noUpdate = true;
      addList.forEach((d) => {
        tableRef == null ? void 0 : tableRef.toggleRowSelection(d, true);
      });
      removeList.forEach((d) => {
        tableRef == null ? void 0 : tableRef.toggleRowSelection(d, false);
      });
      this.noUpdate = false;
    },
    clearRowSelections() {
      var _a, _b;
      this.noUpdate = true;
      (_b = (_a = this.$refs) == null ? void 0 : _a.table) == null ? void 0 : _b.clearSelection();
      this.noUpdate = false;
    },
    getMultipleResult(values) {
      const checked = [];
      const addList = [];
      const hasInit = !this.isEmptyValue(this.initValue);
      values.forEach((value) => {
        const item = this.getRowByValue(value, this.selection);
        if (item) {
          checked.push({ label: item[this.labelKey], value });
          return;
        }
        const row = this.getRowByValue(value);
        if (row) {
          addList.push(row);
          checked.push({ label: row[this.labelKey], value });
          return;
        }
        const label = (() => {
          if (!hasInit) {
            return value;
          }
          const temp = this.getRowByValue(value, this.initValue);
          return temp ? temp[this.labelKey] : value;
        })();
        checked.push({ label, value });
        addList.push({
          [this.valueKey]: value,
          [this.labelKey]: label
        });
      });
      const removeList = this.selection.filter((d) => {
        return !values.includes(d[this.valueKey]);
      });
      return { checked, addList, removeList };
    },
    isEmptyValue(value) {
      return typeof value === "undefined" || value === null || value === "";
    },
    getRowByValue(value, data) {
      var _a, _b, _c;
      if (data) {
        return data.find((d) => d[this.valueKey] === value);
      }
      return this.getRowByValue(value, (_c = (_b = (_a = this.$refs) == null ? void 0 : _a.table) == null ? void 0 : _b.getData()) != null ? _c : []);
    },
    onTableDone() {
      var _a;
      this.$nextTick(() => {
        var _a2, _b;
        (_b = (_a2 = this.$refs) == null ? void 0 : _a2.select) == null ? void 0 : _b.updatePopper();
      });
      if (!this.multiple) {
        if (!this.isEmptyValue(this.value)) {
          const row = this.getRowByValue(this.value);
          if (row) {
            this.selectedLabel = row[this.labelKey];
            this.updateCurrentRow(row);
          }
        }
        return;
      }
      if ((_a = this.value) == null ? void 0 : _a.length) {
        const addList = [];
        const removeList = [];
        this.selection.forEach((d) => {
          const row = this.getRowByValue(d[this.valueKey]);
          if (row) {
            removeList.push(d);
            addList.push(row);
          }
        });
        this.updateRowSelections({ addList, removeList });
      }
    },
    onRowClick() {
      if (!this.multiple) {
        this.updateVisible(false);
      }
    },
    onInitValueChange() {
      const hasInit = !this.isEmptyValue(this.initValue);
      if (hasInit) {
        if (this.multiple) {
          this.updateValue(this.initValue.map((d) => d[this.valueKey]));
        } else {
          this.updateValue(this.initValue[this.valueKey]);
        }
      }
    },
    reload(option) {
      var _a, _b;
      (_b = (_a = this.$refs) == null ? void 0 : _a.table) == null ? void 0 : _b.reload(option);
    }
  },
  mounted() {
    var _a;
    const hasInit = !this.isEmptyValue(this.initValue);
    if (this.multiple) {
      if (hasInit) {
        this.onInitValueChange();
      } else if ((_a = this.value) == null ? void 0 : _a.length) {
        this.onValueChange(this.value);
      }
    } else if (hasInit) {
      this.onInitValueChange();
    } else if (!this.isEmptyValue(this.value)) {
      this.onValueChange(this.value);
    }
  },
  watch: {
    value(value) {
      this.onValueChange(value);
    },
    initValue() {
      this.onInitValueChange();
    }
  },
  render(h) {
    return h("EleBasicSelect", {
      ref: "select",
      props: {
        value: this.value,
        multiple: this.multiple,
        disabled: this.disabled,
        size: this.size,
        clearable: this.clearable,
        collapseTags: this.collapseTags,
        placeholder: this.placeholder,
        automaticDropdown: this.automaticDropdown,
        popperClass: this.selectPopperClass,
        popperOptions: this.popperOptions,
        popperWidth: this.popperWidth,
        id: this.id,
        name: this.name,
        autocomplete: this.autocomplete,
        selectedLabel: this.selectedLabel,
        selected: this.selected,
        visible: this.visible
      },
      on: {
        clear: this.onClear,
        "remove-tag": this.onRemoveTag,
        focus: this.onFocus,
        blur: this.onBlur,
        "update:visible": this.updateVisible
      }
    }, [
      h("EleProTable", {
        ref: "table",
        props: Object.assign({}, this.tableConfig, {
          rowKey: this.valueKey,
          current: this.current,
          selection: this.selection,
          highlightCurrentRow: !this.multiple
        }),
        on: {
          done: this.onTableDone,
          "row-click": this.onRowClick,
          "update:current": this.updateCurrent,
          "update:selection": this.updateSelection
        },
        scopedSlots: this.$scopedSlots
      })
    ]);
  }
};
export { index as default };
